<script setup>
import { ref, reactive, onMounted, nextTick, onUnmounted } from 'vue';
import CommonCharts from '../components/commonCharts.vue';
import GroupCharts from '../components/groupCharts.vue';

const props = defineProps({
  sensors: {
    type: Array,
    required: true,
  },
});
const updateFlag = ref(false);
const Weight = ref(null), Quality = ref(null);
const Wt_data = ref(null), Wt_year = ref(null), Wt_month = ref(null), Wt_day = ref(null);
const Qt_data = ref(null), Qt_charts = ref(null);
onMounted(() => {
  const [sensors0, sensors1] = props.sensors;
  Weight.value = sensors0, Quality.value = sensors1;
  const [Weight0, Weight1, Weight2, Weight3] = Weight.value?.c || [];
  Wt_data.value = Weight0, Wt_year.value = Weight1, Wt_month.value = Weight2, Wt_day.value = Weight3;
  const [Quality0, Quality1] = Quality.value?.c || [];
  Qt_data.value = Quality0, Qt_charts.value = Quality1;
});

const cTimer = setInterval(() => {
  updateFlag.value = true;
  nextTick(() => {
    updateFlag.value = false;
  });
}, 20000);

/* 自增 */
const selfAddRt = reactive({
  dex: 0,
  rate: 0,
  timer: null,
  interval: null,
});
onMounted(() => {
  selfAddRt.timer = setTimeout(() => createNumsAdd(), 2500);
});
const selIndexClick = (num) => {
  selfAddRt.dex = num;
  selfAddRt.rate = 0;
  if (selfAddRt.timer) {
    clearTimeout(selfAddRt.timer);
  }
  selfAddRt.timer = setTimeout(() => createNumsAdd(), 300);
}
const createNumsAdd = () => {
  selfAddRt.interval = setInterval(() => {
    if (selfAddRt.rate <= 0.97) {
      selfAddRt.rate += 0.03;
    } else {
      selfAddRt.rate = 1;
      clearInterval(selfAddRt.interval);
    }
  }, 30);
}
onUnmounted(() => {
  if (selfAddRt.interval) {
    clearInterval(selfAddRt.interval);
  }
  if (selfAddRt.timer) {
    clearTimeout(selfAddRt.timer);
  }
})
</script>

<template>
  <div class="side left" v-if="Weight">
    <div class="water_tab">
      <span @click="selIndexClick(0)" :class="{'isActive' : selfAddRt.dex === 0}">{{ Weight.n }}</span>
      <span @click="selIndexClick(1)" :class="{'isActive' : selfAddRt.dex === 1}">{{ Quality.n }}</span>
    </div>
    
    <template v-if="selfAddRt.dex === 0">
      <template v-if="Wt_data">
        <div class="title">
          <img :src="require('../assets/title.png')" />
          <label>{{ Wt_data.n }}</label>
        </div>
        
        <div class="dataBox4">
          <div class="dataItem" v-for="i in Wt_data.c">
            <div class="data">
              <span>{{ (i.value * selfAddRt.rate).toFixed(2) }}</span>
            </div>
            <label>{{ i.n }}({{ i.unit }})</label>
          </div>
        </div>
      </template>
      
      <template v-if="Wt_year">
        <div class="title" style="marginTop: 20px;">
          <img :src="require('../assets/title.png')" />
          <label>{{ Wt_year.n }}</label>
        </div>
        
        <div class="chartsBox">
          <CommonCharts :params="Wt_year.c" :updateFlag="updateFlag" />
        </div>
      </template>
    </template>
    
    <template v-if="selfAddRt.dex === 1">
      <template v-if="Qt_data">
        <div class="title">
          <img :src="require('../assets/title.png')" />
          <label>{{ Qt_data.n }}</label>
        </div>
        
        <div class="dataBox5">
          <div class="dataItem" v-for="i in Qt_data.c">
            <div class="dataRow">
              <div class="circle">
                <div>{{ i.c[0].n }}</div>
              </div>
              <span>{{ i.c[0].value }}</span>
              <template v-if="i.c.length > 1">
                <div class="line">|</div>
                <span>{{ i.c[1].value }}</span>
                <div class="circle">
                  <div>{{ i.c[1].n }}</div>
                </div>
              </template>
            </div>
            <span>{{ i.n }}({{ i.c[0].unit }})</span>
            <img :src="require('../assets/data_bg_qt.png')" />
          </div>
        </div>
      </template>
      
      <template v-if="Qt_charts">
        <div class="title">
          <img :src="require('../assets/title.png')" />
          <label>{{ Qt_charts.n }}</label>
        </div>
        
        <div class="chartsBox">
          <GroupCharts :params="Qt_charts.c" :updateFlag="updateFlag" />
        </div>
        
      </template>
    </template>
  </div>
  
  <div class="side right">
    <template v-if="selfAddRt.dex === 0">
      <template v-if="Wt_month">
        <div class="title">
          <img :src="require('../assets/title.png')" />
          <label>{{ Wt_month.n }}</label>
        </div>
        
        <div class="chartsBox">
          <CommonCharts :params="Wt_month.c" :updateFlag="updateFlag" />
        </div>
      </template>
      
      <template v-if="Wt_day">
        <div class="title" style="marginTop: 100px;">
          <img :src="require('../assets/title.png')" />
          <label>{{ Wt_day.n }}</label>
        </div>
        
        <div class="chartsBox">
          <CommonCharts :params="Wt_day.c" :updateFlag="updateFlag" />
        </div>
      </template>
      
    </template>
  </div>
</template>

<style lang="scss" scoped>
@font-face {
  font-family: "PangMenZhengDao";
  src: url("/font/庞门正道标题体3.0.ttf");
}
@font-face {
  font-family: "YouSheBiaoTiHei";
  src: url("/font/优设标题黑.ttf");
}
.side {
  width: 376px;
  position: absolute;
  top: 0;
  bottom: 0;
  padding-top: 42px;
  z-index: 1;
}
.left {
  left: 0;
  // background-color: #ff0000;
  background: linear-gradient(90deg, #002324 0%, rgba(0,0,0,0) 93%);
  animation: lEnter 1s linear 1.5s;
  -webkit-animation: lEnter 1s linear 1.5s;
  animation-fill-mode: backwards;
  -webkit-animation-fill-mode: backwards;
  padding-left: 17px;
}
.right {
  right: 0;
  // background-color: #ff0000;
  background: linear-gradient(270deg, #002324 0%, rgba(0,0,0,0) 93%);
  animation: rEnter 1s linear 1.5s;
  -webkit-animation: rEnter 1s linear 1.5s;
  animation-fill-mode: backwards;
  -webkit-animation-fill-mode: backwards;
  padding-right: 17px;
}
@keyframes rEnter {
  0% {
    transform: translateX(539px);
    opacity: 0;
  }
  100% {
    transform: translateX(0px);
    opacity: 1;
  }
}
@keyframes lEnter {
  0% {
    transform: translateX(-539px);
    opacity: 0;
  }
  100% {
    transform: translateX(0px);
    opacity: 1;
  }
}
.title {
  width: 100%;
  height: 39px;
  line-height: 39px;
  position: relative;
  img {
    position: absolute;
    width: 100%;
    height: 100%;
    opacity: 0.7;
  }
  label {
    position: absolute;
    left: 71px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 23px;
    font-family: YouSheBiaoTiHei;
    font-weight: 400;
    color: #FFFFFF;
    letter-spacing: 2px;
  }
}
.water_tab {
  position: absolute;
  right: 33px;
  top: 63px;
  height: 27px;
  display: flex;
  transform: translateY(-50%);
  border: 1px solid #0EDEED;
  border-radius: 3px;
  z-index: 3;
  span {
    width: 45px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    font-size: 14px;
    font-weight: 500;
    color: #FFFFFF;
    border-radius: 3px;
    cursor: pointer;
  }
  .isActive {
    background: #04B2BF;
  }
}
.dataBox4 {
  width: 100%;
  min-height: 300px;
  padding: 10px 0;
  text-align: center;
  display: flex;
  flex-wrap: wrap;
  .dataItem {
    width: 112px;
    height: 124px;
    margin-top: 15px;
    margin-bottom: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .dataItem:nth-child(2n-1) {
    margin-left: 19px;
    margin-right: 45px;
  }
  .dataItem:nth-child(2n) {
    margin-left: 45px;
    margin-right: 19px;
  }
  .data {
    width: 100%;
    height: 91px;
    background: url('../assets/data_bg.png');
    background-size: 100% 100%;
    position: relative;
    span {
      position: absolute;
      top: 21px;
      left: 50%;
      transform: translateX(-50%);
      font-size: 22px;
      font-family: PangMenZhengDao;
      font-weight: 400;
      color: #FFFFFF;
      line-height: 26px;
    }
  }
  label {
    margin-top: 16px;
    text-align: center;
    font-size: 12px;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 14px;
  }
}
.chartsBox {
  width: 100%;
  height: 250px;
  padding-top: 32px;
}

.dataBox5 {
  width: 100%;
  height: 380px;
  text-align: center;
  position: relative;
  .dataItem {
    width: 50%;
    height: 106px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .dataRow {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 15px;
      .line {
        font-family: PangMenZhengDao;
        color: #FFFFFF;
        margin: 0 8px;
      }
      .circle {
        background-color: #14B9C5;
        border-radius: 50%;
        width: 13px;
        height: 13px;
        margin: 0 5px;
        position: relative;
        >div {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%) scale(0.7);
          font-size: 12px;
          color: #FFFFFF;
        }
      }
      >span {
        font-size: 15px;
        font-family: PangMenZhengDao;
        color: #FFFFFF;
        height: 13px;
        line-height: 13px;
      }
    }
    >span {
      margin-top: 8px;
      font-size: 12px;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 14px;
    }
    img {
      margin-top: 5px;
      width: 94px;
      height: 63px;
    }
  }
  .dataItem:nth-child(1) {
    position: absolute;
    left: 0;
    top: 30px;
  }
  .dataItem:nth-child(2) {
    position: absolute;
    right: 0;
    top: 30px;
  }
  .dataItem:nth-child(3) {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .dataItem:nth-child(4) {
    position: absolute;
    left: 0;
    bottom: 30px;
  }
  .dataItem:nth-child(5) {
    position: absolute;
    right: 0;
    bottom: 30px;
  }
}
</style>